//圆
//x1 = x0 + r * Math.cos(angle * Math.PI / 180)
//y1 = y0 + r * Math.sin(angle * Math.PI / 180)
//x0,y0 圆心
//球

let config = {
    width: 700,//宽度
    height: 700,//高度
    tagsNum: 20,//标签数量
    RADIUS: 200,//球的半径
    speedX: Math.PI / 360,//球一帧绕x轴旋转的角度
    speedY: Math.PI / 360,//球-帧绕y轴旋转的角度
    tags: [],
    CX:width/2,
    CY:height/2,
    init:function(){
        for(let i = 0; i < this.tagsNum; i++){
            let tag = {};
            let k = -1 + (2 * (i + 1) - 1) / this.tagsNum;
            let a = Math.acos(k);
            let b = a * Math.sqrt(this.tagsNum * Math.PI)//计算标签相对于球心的角度
            tag.text = i + 'tag';
            tag.x = this.CX +  this.RADIUS * Math.sin(a) * Math.cos(b);//根据标签角度求出标签的x,y,z坐标
            tag.y = this.CY +  this.RADIUS * Math.sin(a) * Math.sin(b); 
            tag.z = this.RADIUS * Math.cos(a);
            tag.href = 'https://imgss.github.io';//给标签添加链接
            this.tags.push(tag);
        }
    },
    rotateX(angleX){
        var cos = Math.cos(angleX);
        var sin = Math.sin(angleX);
        for(let tag of this.tags){
            var y1 = (tag.y- this.CY) * cos - tag.z * sin  + this.CY;
            var z1 = tag.z * cos + (tag.y- this.CY) * sin;
            tag.y = y1;
            tag.z = z1;
        }
    },
    rotateY(angleY){
        var cos = Math.cos(angleY);
        var sin = Math.sin(angleY);
        for(let tag of this.tags){
            var x1 = (tag.x - this.CX) * cos - tag.z * sin + this.CX;
            var z1 = tag.z * cos + (tag.x - this.CX) * sin;
            tag.x = x1;
            tag.z = z1;
        }
    },
    update:function(event){
        var x = event.clientX - this.CX;
        var y = event.clientY - this.CY;
        this.speedX = x*0.0001>0 ? Math.min(this.RADIUS*0.00002, x*0.0001) : Math.max(-this.RADIUS*0.00002, x*0.0001);
        this.speedY = y*0.0001>0 ? Math.min(this.RADIUS*0.00002, y*0.0001) : Math.max(-this.RADIUS*0.00002, y*0.0001); 
        this.rotateX(this.speedX);
        this.rotateY(this.speedY);
    }

}